package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
)

func main() {
	getCurrentFilePath()
	fmt.Println()
	getProjectPath()
	fmt.Println()
	calRelativePath()
}

// getCurrentFilePath
//
//	@Description: 获取当前文件的路径
//	@return string
func getCurrentFilePath() {
	fmt.Println("-------------获取当前文件的路径-------------")
	_, path, _, _ := runtime.Caller(0)
	fmt.Println(path)
	//E:/goprojects/src/helloworld01/src/helloword01/24file/file01/file01.go

}

// getProjectPath
//
//	@Description: 获取project路径
//	@return string
func getProjectPath() {
	fmt.Println("-------------获取project路径-------------")
	path, err := os.Getwd()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("projectPath=", path)
	//E:\goprojects\src\helloworld01\src\helloword01

}

// calRelativePath
//
//	@Description: 根据相对路径获取绝对路径
func calRelativePath() {
	fmt.Println("-------------根据相对路径获取绝对路径-------------")
	_, currentFilePath, _, _ := runtime.Caller(0)
	fmt.Println("当前文件路径=", currentFilePath)
	var relativePath = "../../00_testfile/001.png"
	fmt.Println("相对路径=", relativePath)
	//根据当前文件路径获取指定文件的路径
	path := filepath.Join(currentFilePath, relativePath)
	//E:\goprojects\src\helloworld01\src\helloword01\24file\00_testfile\001.png
	fmt.Println(path)
}
